<?php

		/* Default form to existing config settings */
		if(file_exists("config.php") && $_POST['action'] != 'generate'){
			include("config.php");
			$type= SQL_TYPE;
			$name= SQL_DB_NAME;
			$user= SQL_USER;
			$pass= SQL_PASS;
			$host= SQL_HOST;
		}

		 if($_POST['action'] == 'generate') {
		 	$type= $_POST['db_type'];
			$name= $_POST['db_name'];
			$user= $_POST['username'];
			$pass= $_POST['db_pass'];
			$host= $_POST['host'];
			
			switch($type) {
				case "mysql":
					$sql_port = 3306;
					break;
				case "pgsql":default:
					$sql_port = 5432;
					break;
			}
			
			if(isset($type) && isset($name) && isset($user) && isset($pass) && isset($host)) {
				/* It's damn ugly but... */
				$config_content= "<?php\n/* Planetoid configuration file */\n/* Generated by Planetoid on ".date('r')." */\n\n"
					."define('SQL_TYPE', '{$type}');\n"
					."define('SQL_USER', '{$user}');\n"
					."define('SQL_PASS', '{$pass}');\n"
					."define('SQL_DB_NAME', '{$name}');\n"
					."define('SQL_HOST', '{$host}');\n"
					."define('SQL_PORT', {$sql_port});\n?>";
				
				if(is_writable('config.php')) {
					$config= fopen('config.php', 'w+');
// 					ftruncate($config, filesize($config));
					sleep(1);
					fwrite($config, $config_content);
					fclose($config);
					$configured= true;
				} else {
					$error= "config.php is not writeable! Try: <pre>chmod 777 config.php</pre>";
				}
			} else {
				$error= "You have to fill all fields";
			}
		 }
		 ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head profile="http://gmpg.org/xfn/11">
		<meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8" />
		<title>Planetoid configuration</title>
		<link href="admin/inc/css/login-install.css" rel="stylesheet" />
		<link href="admin/inc/favicon.css" rel="icon" />
		<link href="admin/inc/favicon.css" rel="shortcut icon" />
	</head>
	<body>
		<form action="config-config.php" method="POST" class="install">
			<img src="admin/inc/images/logo-login.png" alt="Planetoid's logo" />
			<?php if(isset($error)): ?>
			<div class="error">
				<?=$error?>
			</div>
			<?php endif; ?>
			<div class="info-info">
				<?php if($configured) { ?>
				Your configuration file has been saved.
				<?php } else { ?>
				Here you can configure your config.php before you install Planetoid.
				<?php }; ?>
			</div>
			<hr/>
			<label for="db_type">Database:</label>
			<select name="db_type" id="db_type">
				<option value="pgsql" <?php if (SQL_TYPE == "pgsql") echo "selected" ?>>PostgreSQL</option>
				<option value="mysql" <?php if (SQL_TYPE == "mysql") echo "selected" ?>>MySQL</option>
			</select>
			
			<label for="host">Host:</label>
			<input type="text" name="host" id="host" value="localhost" /><br/>
			
			<label for="db_name">Database name:</label>
			<input type="text" name="db_name" id="db_name" value="<?=$db_name?>" /><br/>
			
			<label for="username">Username:</label>
			<input type="text" name="username" id="username" value="<?=$db_type?>" /><br/>
			
			<label for="db_pass">Password:</label>
			<input type="password" name="db_pass" id="db_pass" value="<?=$db_pass?>" /><br/>
			
			<input type="hidden" name="action" value="generate" />
			<p>
				<small><a href="install.php">Install Planetoid</a></small>
				<input type="submit" value="Save configuration &raquo;" />
			</p>
		</from>
	</body>
</html>
